package com.ruoyi.energy.service.impl;

import com.ruoyi.common.core.exception.CustomException;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.SecurityUtils;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.utils.bean.BeanUtils;
import com.ruoyi.energy.domain.NyEnteriseLoad;
import com.ruoyi.energy.domain.NyTrans;
import com.ruoyi.energy.domain.NyTransPart;
import com.ruoyi.energy.domain.NyTransPartArrs;
import com.ruoyi.energy.domain.vo.NyTransPartExcelVO;
import com.ruoyi.energy.mapper.NyTransPartArrsMapper;
import com.ruoyi.energy.mapper.NyTransPartMapper;
import com.ruoyi.energy.service.INyTransPartArrsService;
import com.ruoyi.energy.service.INyTransPartService;
import com.ruoyi.energy.service.INyTransService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 变压器-部件Service业务层处理
 *
 * @author cjie
 * @date 2021-10-11
 */
@Service
public class NyTransPartServiceImpl implements INyTransPartService {
    @Autowired
    private NyTransPartMapper nyTransPartMapper;

    @Autowired
    private INyTransService nyTransService;

    @Autowired
    private NyTransPartArrsMapper nyTransPartArrsMapper;

    /**
     * 查询变压器-部件
     *
     * @param id 变压器-部件ID
     * @return 变压器-部件
     */
    @Override
    public NyTransPart selectNyTransPartById(Long id) {
        return nyTransPartMapper.selectNyTransPartById(id);
    }

    /**
     * 查询变压器-部件列表
     *
     * @param nyTransPart 变压器-部件
     * @return 变压器-部件
     */
    @Override
    public List<NyTransPart> selectNyTransPartList(NyTransPart nyTransPart) {
        return nyTransPartMapper.selectNyTransPartList(nyTransPart);
    }

    /**
     * 新增变压器-部件
     *
     * @param nyTransPart 变压器-部件
     * @return 结果
     */
    @Override
    public int insertNyTransPart(NyTransPart nyTransPart) {
        //nyTransPart.setId(IdUtil.getSnowflake(0, 0).nextIdStr());
        nyTransPart.setCreateTime(DateUtils.getNowDate());
        nyTransPart.setCreateBy(SecurityUtils.getUsername());
        return nyTransPartMapper.insertNyTransPart(nyTransPart);
    }

    /**
     * 修改变压器-部件
     *
     * @param nyTransPart 变压器-部件
     * @return 结果
     */
    @Override
    public int updateNyTransPart(NyTransPart nyTransPart) {
        nyTransPart.setUpdateTime(DateUtils.getNowDate());
        nyTransPart.setUpdateBy(SecurityUtils.getUsername());
        return nyTransPartMapper.updateNyTransPart(nyTransPart);
    }

    /**
     * 批量删除变压器-部件
     *
     * @param ids 需要删除的变压器-部件ID
     * @return 结果
     */
    @Override
    public int deleteNyTransPartByIds(Long[] ids) {
        return nyTransPartMapper.deleteNyTransPartByIds(ids);
    }

    /**
     * 删除变压器-部件信息
     *
     * @param id 变压器-部件ID
     * @return 结果
     */
    @Override
    public int deleteNyTransPartById(Long id) {
        return nyTransPartMapper.deleteNyTransPartById(id);
    }

    @Override
    public String importData(List<NyTransPartExcelVO> buiRoomVOList, String meterType) {
        if (StringUtils.isNull(buiRoomVOList) || buiRoomVOList.size() == 0) {
            throw new CustomException("导入抄表数据不能为空！");
        }
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        buiRoomVOList = buiRoomVOList.stream().filter(o->StringUtils.isNotBlank(o.getTransName())).collect(Collectors.toList());
        this.insertDataBatch(buiRoomVOList,meterType);
        successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + buiRoomVOList.size() + " 条，数据如下：");
        return successMsg.toString();
    }
    //批量插入数据
    private void insertDataBatch(List<NyTransPartExcelVO> buiRoomVOList, String meterType){
        //根据箱变编码查询箱变，不存在插入箱变，存在部件数据，存在更新数据，不存在插入数据。
        //查询箱变列表，HashMap
        Map<String,NyTrans> nyTransMap = nyTransService.selectNyTransList(new NyTrans()).stream().collect(Collectors.toMap(o->o.getNum(), a->a));
        NyTransPart target = new NyTransPart();
        NyTransPart search = new NyTransPart();
        for ( NyTransPartExcelVO vo1: buiRoomVOList) {
            BeanUtils.copyBeanProp(target,vo1);

            if(nyTransMap.get(vo1.getTransNum()) != null){
                target.setTransId(nyTransMap.get(vo1.getTransNum()).getId());
            }else{
                //insert NyTrans
                NyTrans nyTrans = new NyTrans();
                nyTrans.setNum(vo1.getTransNum());
                nyTrans.setName(vo1.getName());
                nyTransService.insertNyTrans(nyTrans);
                //map put
                nyTransMap.put(nyTrans.getNum(),nyTrans);
                target.setTransId(nyTrans.getId());
            }
            search.setTransId(target.getTransId());
            search.setType(target.getType());
            search.setName(target.getName());
            search.setModal(target.getModal());
            search.setDiameter(target.getDiameter());
            List<NyTransPart> partList = nyTransPartMapper.selectNyTransPartList(search);
            if(partList !=null && partList.size()>0){
                target.setId(partList.get(0).getId());
                this.updateNyTransPart(target);
            }else {
                this.insertNyTransPart(target);
            }
//            if(target.getType() !=null && target.getType().equals("空开") ){
//                //插入 空开使用 详情，使用单位，审批负荷，部件id
//                NyTransPartArrs nyTransPartArrs = nyTransPartArrsMapper.selectNyTransPartArrsByAirId(target.getId());
//                if(nyTransPartArrs == null){
//                    nyTransPartArrs = new NyTransPartArrs();
//                    nyTransPartArrs.setTransId(target.getTransId());
//                    nyTransPartArrs.setEnAllow(vo1.getEnAllow());
//                    nyTransPartArrs.setEn(vo1.getUser());
//                    nyTransPartArrs.setStatus(vo1.getUseState());
//                    nyTransPartArrs.setAirId(target.getId());
//                    nyTransPartArrs.setCreateBy(SecurityUtils.getUsername());
//                    nyTransPartArrs.setCreateTime(DateUtils.getNowDate());
//                    nyTransPartArrsMapper.insertNyTransPartArrs(nyTransPartArrs);
//                    this.insertNyEnteriseLoad(nyTransPartArrs);
//                }else{
//                    if(!(vo1.getUser() == null && vo1.getEnAllow() == null)){
//                        if(!vo1.getUser().equals(nyTransPartArrs.getEn()) || !vo1.getEnAllow().equals(nyTransPartArrs.getEnAllow())){
//                            nyTransPartArrs.setEnAllow(vo1.getEnAllow());
//                            nyTransPartArrs.setEn(vo1.getUser());
//                            nyTransPartArrsMapper.deleteNyEnteriseLoadByAttrId(nyTransPartArrs.getAttrId());
//                            nyTransPartArrsMapper.updateNyTransPartArrs(nyTransPartArrs);
//                            this.insertNyEnteriseLoad(nyTransPartArrs);
//                        }
//                    }
//                }
//            }
        }
    }
    // 重新设计表结构，调整页面，适应抄录。总表抄录

    private void insertNyEnteriseLoad(NyTransPartArrs nyTransPartArrs) {
        String ens = nyTransPartArrs.getEn();
        String enAllows = nyTransPartArrs.getEnAllow();
        if (StringUtils.isNotNull(ens) && StringUtils.isNotNull(enAllows) ){
            Long attrId = nyTransPartArrs.getAttrId();
            List<NyEnteriseLoad> list = new ArrayList<NyEnteriseLoad>();
            String[] res1 = ens.split("/");
            String[] res2 = enAllows.split("/");

            for(int i=0;i<res1.length;i++){
                NyEnteriseLoad nyEnteriseLoad= new NyEnteriseLoad();
                nyEnteriseLoad.setAttrId(attrId);
                nyEnteriseLoad.setEntepriseName(res1[i]);
                if(i<res2.length){
                    nyEnteriseLoad.setApprovalLoad(Integer.parseInt(res2[i]));
                }
                list.add(nyEnteriseLoad);
            }

            if (list.size() > 0) {
                nyTransPartArrsMapper.batchNyEnteriseLoad(list);
            }
        }

    }

}
